package com.xxl.job.admin.controller;

import com.alibaba.fastjson.JSON;
import com.xxl.job.admin.controller.annotation.PermissionLimit;
import com.xxl.job.admin.dao.DbInfoDao;
import com.xxl.job.admin.service.DataCenterService;
import com.xxl.job.core.biz.model.DbInfo;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.constants.Constants;
import com.xxl.job.core.enums.DbinfoBelongEnum;
import com.xxl.job.core.util.JdbcUtil;
import com.xxl.job.core.util.ListUtils;
import freemarker.core.ReturnInstruction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/10/5.
 */

@Controller
@RequestMapping("/datacenter")
public class DataCenterController {

    @Resource
    private DbInfoDao dbInfoDao;

    @Resource
    private DataCenterService dataCenterService;

    @RequestMapping
    @PermissionLimit(adminuser = true)
    public String index(Model model) {
        List<DbInfo> dbInfos = dbInfoDao.queryByDataType(Constants.DB_TYPE_MYSQL);
        model.addAttribute("dbInfos", dbInfos);
        /*if(ListUtils.isNotEmpty(dbInfos)) {
            DbInfo dbInfo = dbInfos.get(0);
            List<Map<String, Object>> tables = JdbcUtil.getAllTables(dbInfo);
            System.out.println("DataCenterController.index" + JSON.toJSONString(tables));
            model.addAttribute("tables", tables);
        }*/
        return "datacenter/datacenter.index";
    }

    @RequestMapping(value = "/searchDataInfo")
    @ResponseBody
    public ReturnT<List<DbInfo>> searchDataInfo(@RequestParam(value = "dbType") String dbType) {
        List<DbInfo> dbInfos = dbInfoDao.queryByDataType(dbType);
        if(ListUtils.isNotEmpty(dbInfos)) {
            return new ReturnT<>(dbInfos);
        }
        return new ReturnT<>(ReturnT.FAIL_CODE , "");
    }

    @RequestMapping(value = "/searchTableName")
    @ResponseBody
    public ReturnT<List<Map<String, Object>>> searchTableName(@RequestParam(value = "dbType") String dbType, @RequestParam(value = "dbInfo") Long dbInfo) {
        List<DbInfo> dbInfos = dbInfoDao.queryByDataType(dbType);
        for(DbInfo dbInfo1 : dbInfos) {
            if(dbInfo==dbInfo1.getId()) {
                List<Map<String, Object>> tables = JdbcUtil.getAllTables(dbInfo1);
                System.out.println("DataCenterController.index" + JSON.toJSONString(tables));
                return new ReturnT<>(tables);
            }
        }
        return new ReturnT<>(ReturnT.FAIL_CODE , "");
    }

    @RequestMapping(value = "/export")
    @ResponseBody
    public ReturnT<String> export(@RequestParam(value = "dbInfo") Long dbInfo,
                                  @RequestParam(value = "tableName") String tableName,
                                  @RequestParam(value = "sheetName") String sheetName,
                                  HttpServletRequest request) {
        return dataCenterService.exportToFile(request, dbInfo, tableName, sheetName);
    }

    @RequestMapping(value = "/open")
    @ResponseBody
    public ReturnT<String> open(@RequestParam(value = "filePath") String filePath) {
        return dataCenterService.open(filePath);
    }

    @RequestMapping(value = "/openDirectory")
    @ResponseBody
    public ReturnT<String> openDirectory(@RequestParam(value = "filePath") String filePath) {
        return dataCenterService.openDirectory(filePath);
    }

    @RequestMapping(value = "/searchTableFields")
    @ResponseBody
    public ReturnT<List<Map<String, Object>>> searchTableFields(@RequestParam(value = "dbInfo") Long dbInfo,
                                  @RequestParam(value = "tableName") String tableName) {
        return dataCenterService.searchTableFields(dbInfo, tableName);
    }

    @RequestMapping(value = "/pageList")
    @ResponseBody
    public ReturnT<List<Map<String, Object>>> pageList(@RequestParam(required = false, defaultValue = "0") int start,
                                                       @RequestParam(required = false, defaultValue = "10") int length,
                                                       @RequestParam(value = "dbInfo") Long dbInfo,
                                                       @RequestParam(value = "tableName") String tableName) {
        return dataCenterService.pageList(dbInfo, tableName, start, length);
    }
}
